﻿using System;
using System.Data.Common;
using System.Reflection;
using System.Data.Sql;
using System.Data.SqlClient;
using iSun.Log;

namespace ZX.DAL.DataFactory
{
    internal class SqlServer : DataBase
    {
        #region 构造函数
        private static DbProviderFactory _dbProviderFactory;
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static DbProviderFactory dbProviderFactory
        {
            get
            {
                if (_dbProviderFactory == null)
                {
                    //反射实现获取数据库工厂
                    Assembly asm = Assembly.LoadWithPartialName("System.Data.SqlClient");
                    Type type = asm.GetType("SqlClientFactory");
                    FieldInfo field = type.GetField("Instance");
                    _dbProviderFactory =  field.GetValue(null)   as DbProviderFactory;
                }
                return _dbProviderFactory;
            }
        }

 

        /// <summary>
        /// 构造函数。自己创建Conn对象
        /// </summary>
        /// <param name="connStr">连接字符串</param>
        public SqlServer(String connStr)
            : base("", _dbProviderFactory)
        {
            if (!String.IsNullOrEmpty(connStr))
            {
                Conn = _dbProviderFactory.CreateConnection();
                Conn.ConnectionString = connStr;
            }
        }


        /// <summary>
        /// 构造函数。使用外部连接
        /// </summary>
        /// <param name="conn">连接</param>
        public SqlServer(DbConnection conn) : base(conn, _dbProviderFactory) { }
        #endregion


        #region  重写方法
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override DbCommand GetCommand()
        {
            return _dbProviderFactory.CreateCommand();
            //return new System.Data.SqlClient.SqlCommand();
        }

        public override DbCommand GetCommand(string Sql, DbConnection iConn)
        {
            return new System.Data.SqlClient.SqlCommand(Sql,(SqlConnection)iConn);
            //throw new NotImplementedException();
        }

        public override DbConnection GetConnection(DbProviderFactory dpf)
        {
            return new System.Data.SqlClient.SqlConnection(this.ConntionString);
            //return base.GetConnection(dpf);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override DataAdapter GetAdapater()
        {
            return _dbProviderFactory.CreateDataAdapter();
            //return new System.Data.SqlClient.SqlDataAdapter();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="iConn"></param>
        /// <returns></returns>
        public override DataAdapter GetAdapater(string Sql, DbConnection iConn)
        {
            DataAdapter da = _dbProviderFactory.CreateDataAdapter();
            //da.
            return new System.Data.SqlClient.SqlDataAdapter(Sql, (SqlConnection)iConn);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="iCmd"></param>
        /// <returns></returns>

        public override DataAdapter GetAdapater(DbCommand iCmd)
        {
            return new System.Data.SqlClient.SqlDataAdapter((SqlCommand)iCmd);
        }
        #endregion
    }
}
